﻿Public Class frmband
    Dim Strsql As String
    Dim Mysql1 As New ClsSQLhelper
    Dim On_Load As Boolean = True
#Region "Function"
    Function Load_Data(ByVal siteid As Integer, Optional ByVal Str_Search As String = "") As Boolean
        Try
            Dim DS As New DataSet
            Load_Data = False
            Strsql = "select band_id,band_name,manufactur.manufacturname,band.note,band.active from band  left join manufactur on manufactur_id = manufactur.manufacturid where band.siteid = " & siteid
            If Str_Search <> "" Then Strsql = Strsql & " and  band_name like '" & Str_Search & "%' "
            Strsql = Strsql & " order by active desc,band_name"
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_band", "band")
            Pn_Framework.Set_GridSetting(Me.gridband, DS)
            With gridband
                .Columns(0).HeaderText = "รหัส"
                .Columns(1).HeaderText = "ชื่อ"
                .Columns(1).Width = 100
                .Columns(2).HeaderText = "ผู้ผลิต"
                .Columns(2).Width = 100
                .Columns(3).HeaderText = "Note"
                .Columns(3).Width = 100
                .Columns(4).HeaderText = "เปิดใช้งาน"

            End With
            Load_Data = True
            On_Load = False
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Function
    Function Load_ComboboxData_site() As Boolean
        Try
            Dim DS As New DataSet

            Load_ComboboxData_site = False

            Strsql = "select * from site"
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_site", "site")
            ' Me.EventLog1.WriteEntry(Strsql, EventLogEntryType.Information)
            With Me.CboSite
                If DS.Tables(0).Rows.Count = 0 Then Exit Function
                .DataSource = DS.Tables(0)
                .DisplayMember = "site"
                .ValueMember = "siteid"
                .SelectionStart = 0
                If My.Settings.CurrentSite <> "" Then
                    .SelectedValue = My.Settings.CurrentSite
                End If
            End With
            Load_ComboboxData_site = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : Function Load_ComboboxData_site - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)

        End Try
    End Function
#End Region

    Private Sub frmband_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_ComboboxData_site()
        Load_ComboboxData_Manufactur()
        If My.Settings.CurrentuserGroup <> 1 Then Me.CboSite.Enabled = False
        If Load_Data(Me.CboSite.SelectedValue) = False Then
            MsgBox("ระบบพบว่าฐานข้อมูลของคุณไม่ update หรือ ไม่ถูกต้อง", MsgBoxStyle.Exclamation, "กรุณาตรวจสอบฐานข้อมูลของคุณ")
            Exit Sub
        End If
    End Sub


    Private Sub gridband_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridband.CellClick
        Try
            With gridband
                If .RowCount = 0 Then Exit Sub
                Me.Txtid.Text = .Item(0, e.RowIndex).Value.ToString
                Me.Txtname.Text = .Item(1, e.RowIndex).Value.ToString
                Me.Cbomanufactur.Text = IIf(IsDBNull(.Item(2, e.RowIndex).Value.ToString), "ไม่ระบุ", .Item(2, e.RowIndex).Value.ToString)
                Me.TxtNote.Text = .Item(3, e.RowIndex).Value.ToString
                Me.ChkActive.Checked = .Item(4, e.RowIndex).Value
                Me.BtnNew.Enabled = False
                Me.BtnSave.Enabled = True
                Me.BtnDelete.Enabled = True
                Me.TxtNote.ReadOnly = False
                Me.Txtname.ReadOnly = False
                ChkAutoAddManufac.Text = "แก้ไขชื่อผู้ผลิตอัตโนมัติ"
            End With
            With Me.Tabband
                .SelectedTab = TabPage2
            End With
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNew.Click

        Me.Txtid.Text = ""
        Me.TxtNote.Text = ""
        Me.Txtname.Text = ""
        Me.ChkActive.Checked = True
        Me.Txtname.ReadOnly = False
        Me.TxtNote.ReadOnly = False
        Me.BtnSave.Enabled = True
        Me.Cbomanufactur.Enabled = False
        Me.btnManuffind.Enabled = False
        ChkAutoAddManufac.Checked = True
        ChkAutoAddManufac.Text = "เพิ่มชื่อผู้ผลิตอัตโนมัติ"

        With Me.Tabband
            .SelectedTab = TabPage2
        End With
    End Sub
    Function Load_ComboboxData_Manufactur() As Boolean
        Try

            Dim DS As New DataSet
            Load_ComboboxData_Manufactur = False
            Strsql = "select * from manufactur where active = 1  and siteid = " & Me.CboSite.SelectedValue & " order by manufacturname "
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_manufactur", "manufactur")

            With Me.Cbomanufactur
                'If DS.Tables(0).Rows.Count = 0 Then Exit Function
                .DataSource = DS.Tables(0)
                .DisplayMember = "manufacturname"
                .ValueMember = "manufacturid"
            End With
            Load_ComboboxData_Manufactur = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : Function Load_ComboboxData_Manufactur - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Function
    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        Try
            If MsgBox("คุณต้องการลบข้อมูลหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบ") = MsgBoxResult.No Then Exit Sub

            Strsql = "Delete from band where band_id = " & Me.Txtid.Text
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            Mysql1.MySQLExecute(Strsql)
            'Refresh Data
            Load_Data(Me.CboSite.SelectedValue)
            BtnNew_Click(sender, e)
            Me.BtnNew_Click(sender, e)
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Try
            If Me.Txtname.Text.Trim = "" Then Exit Sub
            If MsgBox("คุณต้องการบันทึกข้อมูลหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการบันทึก") = MsgBoxResult.No Then Exit Sub

            Dim DS As New DataSet
            If Me.Txtid.Text = "" Then
                Strsql = "Select * from band where band_name= '" & Me.Txtname.Text & "'"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                DS = Mysql1.GetMYSQLDataset(Strsql, "DS_band", "band")
                If DS.Tables(0).Rows.Count <> 0 Then
                    MsgBox("รายการ นี้มีอยู่แล้วไม่สามารถบันทึกซ้ำได้อีก", MsgBoxStyle.Critical)
                    Exit Sub
                End If

                'Add New Manufac
                Dim My_Manufacid As Integer = 0
                If Me.ChkAutoAddManufac.Checked = True Then
                    Strsql = "Insert into manufactur (manufacturname,note,siteid) values ('" & Me.Txtname.Text & "', '" & Me.TxtNote.Text & "'," & Me.CboSite.SelectedValue & ")"
                    Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                    Mysql1.MySQLExecute(Strsql)
                    Strsql = "Select * from manufactur where manufacturname = '" & Me.Txtname.Text & "'"
                    My_Manufacid = Mysql1.MySQLExecuteScalar(Strsql)
                Else
                    My_Manufacid = Me.Cbomanufactur.SelectedValue
                End If

                Strsql = "Insert into band (band_name,manufactur_id,note,siteid) values ('" & Me.Txtname.Text & "'," & My_Manufacid & ", '" & Me.TxtNote.Text & "'," & Me.CboSite.SelectedValue & ")"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                Mysql1.MySQLExecute(Strsql)

                MsgBox("เพิ่มข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)

            Else

                Strsql = "update band set band_name= '" & Me.Txtname.Text & "',manufactur_id = " & Me.Cbomanufactur.SelectedValue & ", note = '" & Me.TxtNote.Text & "' ,active = " & Me.ChkActive.Checked
                Strsql = Strsql & " where band_id = " & Me.Txtid.Text.Trim
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                Mysql1.MySQLExecute(Strsql)
                'Update Manufac
                If Me.ChkAutoAddManufac.Checked = True Then
                    Strsql = "update manufactur set manufacturname= '" & Me.Txtname.Text & "'"
                    Strsql = Strsql & " where manufacturid = " & Cbomanufactur.SelectedValue
                    Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                    Mysql1.MySQLExecute(Strsql)
                End If
                MsgBox("ปรับปรุงข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information)
                End If
                'Refresh Data
                Load_Data(Me.CboSite.SelectedValue)
                Me.BtnNew.Enabled = True
                BtnNew_Click(sender, e)
                Me.Tabband.SelectedTab = TabPage1
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub gridband_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridband.CellContentClick

    End Sub

    Private Sub TxtSearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtSearch.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnFind_Click(sender, e)
        End If
    End Sub
    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Load_Data(Me.CboSite.SelectedValue, Me.TxtSearch.Text)
    End Sub

    Private Sub CboSite_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboSite.SelectedIndexChanged
        If On_Load = False Then
            Load_Data(Me.CboSite.SelectedValue)
            Me.Txtid.ReadOnly = True
            Me.Tabband.SelectedTab = TabPage1
            Me.BtnNew_Click(sender, e)
        End If
    End Sub

    Private Sub TxtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtSearch.TextChanged

    End Sub

    Private Sub TabPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage1.Click

    End Sub

    Private Sub TabPage2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage2.Click

    End Sub

    Private Sub btnManuffind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnManuffind.Click
        frmmanufactur.ShowDialog()
        Load_ComboboxData_Manufactur()
    End Sub

    Private Sub ChkAutoAddManufac_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkAutoAddManufac.CheckedChanged
        If ChkAutoAddManufac.Checked = False Then
            Me.Cbomanufactur.Enabled = True
            Me.btnManuffind.Enabled = True
        Else
            Me.Cbomanufactur.Enabled = False
            Me.btnManuffind.Enabled = False
        End If
    End Sub

    Private Sub BtnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBack.Click
        With Me.Tabband
            .SelectedTab = TabPage1
        End With
    End Sub

End Class